package org.dromara.finance.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.dromara.finance.domain.bo.BillBo;
import org.dromara.finance.domain.vo.BillVo;
import org.dromara.finance.domain.Bill;
import org.dromara.finance.mapper.BillMapper;
import org.dromara.finance.service.IBillService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.Objects;

/**
 * 收款登记Service业务层处理
 *
 * @author xxy
 * @date 2025-08-19
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class BillServiceImpl implements IBillService {

    private final BillMapper baseMapper;

    /**
     * 查询收款登记
     *
     * @param id 主键
     * @return 收款登记
     */
    @Override
    public BillVo queryById(Long id) {
        BillVo vo = baseMapper.selectVoById(id);
        if (vo == null) {
            return null;
        }

        Long billType = vo.getBillType();
        if (Objects.equals(billType, 1L)) {
            return baseMapper.selectSaleBillById(id);
        } else if (Objects.equals(billType, 2L)) {
            return baseMapper.selectPurchaseBillById(id);
        } else {
            return null;
        }
    }


    /**
     * 分页查询收款登记列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 收款登记分页列表
     */
    @Override
    public TableDataInfo<BillVo> queryPageList(BillBo bo, PageQuery pageQuery) {
        Long billType = bo.getBillType();
        log.info("billType,{}", billType);
        if (Objects.equals(billType, 1L)) {
            Page<BillVo> result = baseMapper.selectSalePage(pageQuery.build(), bo);
            return TableDataInfo.build(result);
        }else {
            Page<BillVo> result = baseMapper.selectPurchasePage(pageQuery.build(), bo);
            return TableDataInfo.build(result);
        }
    }

    /**
     * 查询符合条件的收款登记列表
     *
     * @param bo 查询条件
     * @return 收款登记列表
     */
    @Override
    public List<BillVo> queryList(BillBo bo) {
        LambdaQueryWrapper<Bill> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Bill> buildQueryWrapper(BillBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Bill> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(Bill::getId);
        lqw.eq(bo.getBillAmount() != null, Bill::getBillAmount, bo.getBillAmount());
        lqw.eq(bo.getRecordDate() != null, Bill::getRecordDate, bo.getRecordDate());
        lqw.eq(StringUtils.isNotBlank(bo.getVoucherNum()), Bill::getVoucherNum, bo.getVoucherNum());
        return lqw;
    }

    /**
     * 新增收款登记
     *
     * @param bo 收款登记
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(BillBo bo) {
        Bill add = MapstructUtils.convert(bo, Bill.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改收款登记
     *
     * @param bo 收款登记
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(BillBo bo) {
        Bill update = MapstructUtils.convert(bo, Bill.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Bill entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除收款登记信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
